/*
* (c) by Andreas Kreisl
*
* Sys Monitor - Plugin: Wan / External INet-IP
* Version: 0.2
*
* License: creative commons BY-NC-SA
*/


// --------------------------------------------------------------------------------
// myPlugin Variables (required!)

myPlugins["wan"] = new Object();
myPlugins["wan"]["name"] = getLocalizedString2("mod_sysm_plugin_wan_name");
myPlugins["wan"]["onlyonce"] = true;
myPlugins["wan"]["newObject"] = function (NewSettings) { return new plugin_wan_sensor(NewSettings); }

var cachehit = 0;

// --------------------------------------------------------------------------------
// The Sensore Object

function plugin_wan_sensor(NewSettings) {
	// Required variables
	this.plugin = "wan";
	this.settings = NewSettings;
	this.edit = plugin_wan_edit;
	this.ReDraw = plugin_wan_ReDraw;
	this.frame = new Frame();
	this.frame.window = main;
	this.frame.width = mod_sysmonitor_width;
	//this.frame.style.backgroundColor = "red";


	// Warning about Anonymouse Statistics
	if (preferences.mod_sysmonitor_anonstat.value=="0") {
		anon = alert(getLocalizedString2("mod_sysm_anon_askagain"),getLocalizedString2("mod_sysm_anon_continue"),getLocalizedString2("mod_sysm_anon_cancel"));
		if(anon==1) {
			preferences.mod_sysmonitor_anonstat.value = 1;
			savePreferences();
		}
		if(anon==2) {
			this.canceledplugin = true;
			return false;
		}
	}


	// Init Settings
	if(!this.settings.color) this.settings.color = "#FFFFFF";
	if(!this.settings.updspeed) this.settings.updspeed = 300;
	if(!this.settings.cachehit) this.settings.cachehit = 0;
	if(this.settings.whois!=true) this.settings.whois = false;


	// The Sensor
	this.frame.onMultiClick = function ( ) { openURL("http://www.dnsstuff.com/tools/aboutyou.ch"); };
	this.frame.tooltip = getLocalizedString2("mod_sysm_plugin_wan_tooltip");
	this.whoiscache = new Array();
	this.ip = new class_TextLine(this.frame,0,getLocalizedString2("mod_sysm_plugin_wan_IP"));
	this.isp = new class_TextLine(this.frame,mod_sysmonitor_size,getLocalizedString2("mod_sysm_plugin_wan_ISP"));
	this.ip.data.onMultiClick = function ( ) { plugin_wan_copytoclipboard.call(obj); };
	this.ip.data.tooltip = getLocalizedString2("mod_sysm_plugin_wan_tooltip2");
	this.ip.label.color = this.settings.color;
	this.ip.data.color = this.settings.color;
	this.isp.label.color = this.settings.color;
	this.isp.data.color = this.settings.color;
	cachehit = this.settings.cachehit;


	// Update Timer
	var obj = this;
	this.url = new URL();

	this.timer = new Timer();
	this.timer.interval = this.settings.updspeed;
	this.timer.ticking = 1;
	this.timer.onTimerFired = function ( ) { plugin_wan_getip.call(obj); };
	plugin_wan_getip.call(obj);


	// Calculating sensore height
	if ( this.settings.whois==1 ) { 
		this.isp.data.visible = true;
		this.isp.label.visible = true;
		this.frame.height = this.isp.data.vOffset + (this.isp.data.size * 1/3);
	} else {
		this.isp.data.visible = false;
		this.isp.label.visible = false;
		this.frame.height = this.ip.data.vOffset + (this.ip.data.size * 1/3);
	}
	return true;

// --------------------------------------------------------------------------------
// Helper Functions

	function plugin_wan_edit() {
		var Drive_Edit_DialogFields = Array();
		var FieldID = -1;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_color");
		Drive_Edit_DialogFields[FieldID].type = 'color';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.color;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_updatespeed_label");
		Drive_Edit_DialogFields[FieldID].type = 'popup';
		Drive_Edit_DialogFields[FieldID].optionValue = new Array(180,300,600,900);
		Drive_Edit_DialogFields[FieldID].option = new Array();
		for(var i = 0; i < Drive_Edit_DialogFields[FieldID].optionValue.length ; i++ )
			Drive_Edit_DialogFields[FieldID].option[i] = getLocalizedString2("mod_sysm_plugin_updatespeed_" + Drive_Edit_DialogFields[FieldID].optionValue[i]);

		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.updspeed;

		Drive_Edit_DialogFields[++FieldID] = new FormField();
		Drive_Edit_DialogFields[FieldID].title = getLocalizedString2("mod_sysm_plugin_wan_pref_isp");
		Drive_Edit_DialogFields[FieldID].type = 'checkbox';
		Drive_Edit_DialogFields[FieldID].defaultValue = this.settings.whois;

		var Drive_Edit_DialogResults = form(Drive_Edit_DialogFields, getLocalizedString2("mod_sysm_plugin_preftitle") + myPlugins[this.plugin]["name"]);
		if (Drive_Edit_DialogResults != null) {
			this.settings.color = Drive_Edit_DialogResults[0];
			this.ip.label.color = this.settings.color;
			this.ip.data.color = this.settings.color;
			this.isp.label.color = this.settings.color;
			this.isp.data.color = this.settings.color;
			if ( this.settings.updspeed != Drive_Edit_DialogResults[1] ) {
				this.settings.updspeed = Drive_Edit_DialogResults[1];
				this.timer.interval = this.settings.updspeed;
			}
			if ( this.settings.whois != Drive_Edit_DialogResults[2] ) {
				this.settings.whois = Drive_Edit_DialogResults[2];
				if ( this.settings.whois==1 ) { 
					this.isp.data.visible = true;
					this.isp.label.visible = true;
					this.frame.height = this.isp.data.vOffset + (this.isp.data.size * 1/3);
				} else {
					this.isp.data.visible = false;
					this.isp.label.visible = false;
					this.frame.height = this.ip.data.vOffset + (this.ip.data.size * 1/3);
				}
				mod_sysmonitor_ReDraw();
				if ((this.ip.data.data=="")||((this.isp.data.data=="")&&(this.settings.whois==1))) plugin_wan_getip.call(obj);

			}
		}
		delete Drive_Edit_DialogFields;
		return true;
	}

	function plugin_wan_getip() {
		this.ip.data.data = getLocalizedString2("mod_sysm_plugin_wan_searchip");
		if(preferences.mod_sysmonitor_guid.value=="") {
			mod_sysmonitor_GetGUID();
			this.timer.interval = 1;
		} else {
			this.timer.interval = this.settings.updspeed;
			this.url.location = "http://tools.bluepaw.de/widgets/ip.php?guid=" + preferences.mod_sysmonitor_guid.value + "&cachehit=" + this.settings.cachehit;
			if(mod_sysmonitor_debug>3) print("plugin_wan_getip: " +  this.url.location);
			this.url.fetchAsync( function() { plugin_wan_getip_done.call(obj) } );
		}
	}

	function plugin_wan_getip_done() {
		if(mod_sysmonitor_debug>2) print("plugin_wan_getip_done: " + this.url.response);
		if(this.url.response==200) {
			if( (this.settings.whois == 1) && ( (this.ip.data.data != this.url.result) || ( this.isp.data.data == "" ) ) )  {
				if( ! this.whoiscache[this.url.result] ) {
					this.isp.data.data = getLocalizedString2("mod_sysm_plugin_wan_searchisp");
					this.url.location = "http://tools.bluepaw.de/widgets/whois.php?guid=" + preferences.mod_sysmonitor_guid.value;
					if(mod_sysmonitor_debug>3) print("plugin_wan_getwhois: " +  this.url.location);
					this.url.fetchAsync( function() { plugin_wan_getwhois_done.call(obj) } );
				} else {
					if(mod_sysmonitor_debug>3) print("plugin_wan_getwhois: Cache-Hit! " + this.url.result + " => " + this.whoiscache[this.url.result]);
					this.isp.data.data = this.whoiscache[this.url.result];
					this.settings.cachehit++;
				}
			}
			this.ip.data.data = this.url.result;
		} else {
			this.ip.data.data = getLocalizedString2("mod_sysm_plugin_offline");
		}
	}

	function plugin_wan_getwhois_done() {
		if(mod_sysmonitor_debug>2) print("plugin_wan_getwhois_done: " + this.url.response);
		if(this.url.response==200) {
			this.whoiscache[this.ip.data.data] = this.url.result;
			this.isp.data.data = this.url.result;
		} else {
			this.isp.data.data = getLocalizedString2("mod_sysm_plugin_offline");
		}
	}

	function plugin_wan_copytoclipboard() {
		system.clipboard = this.ip.data.data;
		this.ip.data.opacity = 0;
		var a = new FadeAnimation( this.ip.data, 255, 350, animator.kEaseIn );
		animator.start( a ) ;
	}

	function plugin_wan_ReDraw() {
		this.frame.width = mod_sysmonitor_width;

		this.ip.size = mod_sysmonitor_size;
		this.isp.size = mod_sysmonitor_size;
		this.isp.vOffset = mod_sysmonitor_size;

		if ( this.settings.whois==1 ) { 
			this.frame.height = this.isp.data.vOffset + (this.isp.data.size * 1/3);
		} else {
			this.frame.height = this.ip.data.vOffset + (this.ip.data.size * 1/3);
		}

		return true;
	}

}

// --------------------------------------------------------------------------------
// The END